<?php


namespace app\nav\controller;


use app\nav\model\NavCate;
use app\nav\service\MenuService;
use library\Controller;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;

class Links extends Controller
{
    protected $table = 'nav_links';


    protected $cates;

    /**
     * Links constructor.
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->cates = NavCate::field('id,name')->order('sort asc,id desc')->select();
    }

    /**
     * 链接管理
     * @auth true
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public function index()
    {
        $this->title = '链接管理';
        $query       = $this->_query($this->table)->alias('l')->leftJoin('nav_cate c', 'l.cate = c.id')->field('l.*,c.name as cate_name');
        $query->like('name')->equal('cate');
        $query->order('l.sort, l.id desc')->page();
    }

    public function _index_filter(&$data)
    {

    }

    /**
     * 添加链接
     * @auth true
     * @param MenuService $menuService
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public function add(MenuService $menuService)
    {
        $this->title = '添加链接';
        $this->_form($this->table, 'form');
        $menuService->clear();
    }

    /**
     * 编辑链接
     * @auth true
     * @param MenuService $menuService
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public function edit(MenuService $menuService)
    {
        $this->title = '编辑链接';
        $this->_form($this->table, 'form');
        $menuService->clear();
    }

    /**
     * 删除链接
     * @auth true
     * @param MenuService $menuService
     * @throws Exception
     * @throws PDOException
     */
    public function remove(MenuService $menuService)
    {
        $this->_delete($this->table);
        $menuService->clear();
    }
}